package com.senseuni.common.dialog.indicator;

import android.animation.ValueAnimator;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;

public class LineScaleIndicator extends BaseIndicatorController {

    public static final float SCALE = 1.0f;

    float[] scaleYFloats = new float[]{SCALE,
            SCALE,
            SCALE,
            SCALE,
            SCALE,};

    @Override
    public void draw(Canvas canvas, Paint paint) {
        float translateX = getWidth() / 11f;
        float translateY = getHeight() / 2f;
        for (int i = 0; i < 5; i++) {
            canvas.save();
            canvas.translate((2 + i * 2) * translateX - translateX / 2, translateY);
            canvas.scale(SCALE, scaleYFloats[i]);
            RectF rectF = new RectF(-translateX / 2, -getHeight() / 2.5f, translateX / 2, getHeight() / 2.5f);
            canvas.drawRoundRect(rectF, 5, 5, paint);
            canvas.restore();
        }
    }

    @Override
    public void createAnimation() {
        long[] delays = new long[]{100, 200, 300, 400, 500};

        for (int i = 0; i < 5; i++) {
            final int index = i;
            ValueAnimator scaleAnimator = ValueAnimator.ofFloat(1, 0.4f, 1);
            scaleAnimator.setDuration(1000);
            scaleAnimator.setRepeatCount(-1);
            scaleAnimator.setStartDelay(delays[i]);
            scaleAnimator.addUpdateListener(animation -> {
                scaleYFloats[index] = (Float) animation.getAnimatedValue();
                postInvalidate();
            });
            scaleAnimator.start();
            addAnimator(scaleAnimator);
        }
    }
}
